package com.boot.ys.mybatisplus.Test;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 描述：提供Map的中文翻译成英文表名
 *
 * @Author yangsheng
 * @Date 2025/5/23 10:11
 */

public class TranslationEnglish {

    public static void main(String[] args) {

        Map<String, String> replacementMap = new HashMap<>();
        replacementMap.put("jifenduihuan", "积分兑换");
        replacementMap.put("jifenshangpin", "积分商品");
        replacementMap.put("lajifenlei", "垃圾分类");
        replacementMap.put("lajishibie", "垃圾识别");
        replacementMap.put("yonghu", "用户");


        Map<String, String> newMap = new HashMap<>();

        for (Map.Entry<String, String> entry : replacementMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();

            // 拼接url和参数
            String url = "https://v.api.aa1.cn/api/api-fanyi-yd/index.php?msg=" + value + "&type=1";
            // 文档：https://apiv.aa1.cn/doc/api-fanyi-yd.html
            // 响应数据长这样子：{ "type": "中英互译", "desc": "中文翻译英文", "text": "I love you." }

            String jsonResponse = HttpUtil.get(url);
            JSONObject jsonObject = new JSONObject(jsonResponse);
            String text = jsonObject.getStr("text");
            newMap.put(key, text);
        }


        for (Map.Entry<String, String> entry : newMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            // 全小写，并且替换一个或多个连续空格为单个下划线
            String name1 = replacementMap.get(key);

            String name2 = value.toLowerCase().replaceAll("\\s+", "_");
            name2 = name2.replaceAll("-", "_");
            System.out.println("名称：" + name1 + "  原来的: " + key + " 现在的: " + name2);
        }


        System.out.println();
        System.out.println("转换好的==============================");
        System.out.println("Map<String, String> replacementMap = new HashMap<>();");
        for (Map.Entry<String, String> entry : newMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            // 全小写，并且替换一个或多个连续空格为单个下划线 - 转换为  _
            String result = value.toLowerCase().replaceAll("\\s+", "_");
            result = result.replaceAll("-", "_");
            // 打印原来的和现在的表名放Map里面
            System.out.println("replacementMap.put(\"" + key + "\", \"" + result + "\");" + "     //" + replacementMap.get(key));
        }

        // 校验翻译出来的-vales表名称是否有重复
        HashMap<String, String> map = new HashMap<>();
        for (Map.Entry<String, String> entry : newMap.entrySet()) {
            // 全小写，并且替换一个或多个连续空格为单个下划线
            String result = entry.getValue().toLowerCase().replaceAll("\\s+", "_");

            if (map.get(result) == null) {
                map.put(result, result);
            } else {
                throw new RuntimeException("重复的翻译：" + result);
            }
        }

    }


}

